<?php
/**
 * 轮播图
 *
 * 轮播图controller
 *
 * @author    zhouchao <zhouchao@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */

namespace App\Http\Controllers\Backstage;
use App\Http\Controllers\Controller;
use App\Services\CarouselsService\CarouselsBackService;
use Illuminate\Http\Request;
use Response;

class CarouselsController extends Controller
{
    protected $carouselsService;
    public function __construct(CarouselsBackService $carouselsSer)
    {
        $this->carouselsService = $carouselsSer;
    }

    /**
     * 获取轮播图列表
     * @param Request $request
     * @return $this
     */
    public function getList(){
        $carousels = $this->carouselsService->getList(15);
        return view('backstage.carousels.list')
            ->with('carousels',$carousels);
    }

    /**
     * 填加轮播图
     * @param Request $request
     * @return $this
     */
    public function getAdd()
    {
        return view('backstage.carousels.add');
    }

    /**
     * 添加轮播图
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function postAdd(Request $request){
        $this->validate($request, [
            'carousel_type' =>'required|integer',
            'carousel_image' => 'required|image|max:2048',
            'resource_id' => 'string|max:32|alpha_num',
            'url' => 'url',
            'begin' => 'required|date',
            'end' => 'required|date'
        ]);
        $data = $request->all();
        unset($data['_token']);
        $result = $this->carouselsService->checkParam($data);
        if($result['state'] != 'M00000'){
            return redirect()->back()->with('error_message', $result['message']);
        }
        $result = $this->carouselsService->add_carousel($data);
        if($result['state'] == 'M00000'){
            return redirect()->back()->with('success_message', $result['message']);
        }else{
            return redirect()->back()->with('error_message', $result['message']);
        }
    }

    /**
     * 获取轮播图详情
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function getUpdate($carousel_id){
        $carousel_id = intval($carousel_id);
        $carousel = $this->carouselsService->get_carousel_by_id($carousel_id);
        if(!empty($carousel)){
            return view('backstage.carousels.update')->with('carousel',$carousel);
        }else{
            return redirect()->back()->with('error_message', '未找到轮播图');
        }
    }

    /**
     * 更新轮播图
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function postUpdate(Request $request){
        $this->validate($request, [
            'carousel_id' =>'required|integer|exists:carousels,carousel_id,state,1',
            'carousel_type' =>'required|integer',
            'carousel_image' => 'image|max:2048',
            'resource_id' => 'string|max:32|alpha_num',
            'url' => 'url',
            'begin' => 'required|date',
            'end' => 'required|date'
        ]);
        $data = $request->all();
        unset($data['_token']);
        $result = $this->carouselsService->checkParam($data);
        if($result['state'] != 'M00000'){
            return redirect()->back()->with('error_message', $result['message']);
        }
        $flag = $this->carouselsService->update_carousel($data);
        if($flag['state'] != 'M00000'){
            return redirect()->back()->with('error_message', $flag['message']);
        }else{
            return redirect()->back()->with('success_message', $flag['message']);
        }
    }

    /**
     * 删除轮播图
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function postDelete(Request $request){
        $carousel_id = intval($request->input('carousel_id'));
        $flag = $this->carouselsService->changeState(2,$carousel_id);
        if($flag){
            $result = array('message'=>'删除成功!','result'=>'','state'=>'M00000');
        }else{
            $result = array('message'=>'删除失败!','result'=>'','state'=>'M00001');
        }
        return Response::json($result);
    }

    /**
     * 更改排序
     * @param Request $request
     * @return mixed
     */
    public function postUpdateSort(Request $request){
        //检查
        $sort = intval($request->input('sort'));
        $carousel_id = $request->input('carousel_id');
        $flag = $this->carouselsService->changeSort($carousel_id,$sort);
        if($flag){
            $result = array('message'=>'更改成功!','result'=>'','state'=>'M00000');
        }else{
            $result = array('message'=>'修改失败!','result'=>'','state'=>'M00001');
        }
        return Response::json($result);
    }

    /**
     * 轮播图显示与隐藏
     * @param Request $request
     * @return mixed
     */
    public function postShow(Request $request){
        $state = intval($request->input('state'));
        $carousel_id = $request->input('carousel_id');
        $flag = $this->carouselsService->changeState($state,$carousel_id);
        if($flag){
            $result = array('message'=>'修改成功!','result'=>$state,'state'=>'M00000');
        }else{
            $result = array('message'=>'修改失败!','result'=>'','state'=>'M00001');
        }
        return Response::json($result);
    }

}